﻿using BLL;
using DBUtility;
using IYAKASAPortal.Services;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Web;
using System.Web.UI.WebControls;
using UtilityHelper;

namespace IYAKASAPortal.Statistics
{
    public class StaInfoTools
    {
        public static string GetStationName()
        {
            string strStationName = string.Empty;
            INSPECTION_STATION_INFO_BLL bll = new INSPECTION_STATION_INFO_BLL();
            INSPECTION_STATION_INFO info = bll.GetModel(1);
            strStationName = info.STATION_NAME;
            return strStationName;
        }

        /// <summary>
        /// 是否为摩托车
        /// </summary>
        /// <param name="strHPZL"></param>
        /// <returns></returns>
        public static bool GetIsMTC(string strCLZLDH)
        {
            if (strCLZLDH.Substring(0, 1) == "M")
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public static UserInfo GetUserInfo(string userName)
        {
            UserInfo userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            return userInfo;
        }


        public static bool PortInUse(int port)
        {

            IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] iPEndPoints = iPGlobalProperties.GetActiveTcpListeners();

            foreach (IPEndPoint endPoint in iPEndPoints)
            {
                if (endPoint.Port == port)
                {
                    return false;
                }
            }

            return true;

        }

        public static bool IsMoto(int DispatchId)
        {
            bool isMoto = false;
            try
            {
                string strSQL = string.Format("SELECT RESULT_VEHICLE_INFO.ID,RESULT_VEHICLE_INFO.HPZLDH,CONST_DEFINE.CON_VTYPE FROM RESULT_VEHICLE_INFO "
                                               + "INNER JOIN CONST_DEFINE ON CONST_DEFINE.CON_CODE = RESULT_VEHICLE_INFO.HPZLDH "
                                               + "WHERE CONST_DEFINE.CON_CHNAME = 'HPZL' AND RESULT_VEHICLE_INFO.ID = {0}", DispatchId);
                DataTable dt = new DataTable();
                DbHelper.GetTable(strSQL, ref dt);
                int iCON_VTYPE = Convert.ToInt32(dt.Rows[0]["CON_VTYPE"]);

                if (iCON_VTYPE == 1)
                {
                    isMoto = true;
                }
            }
            catch (Exception ex)
            {

            }

            return isMoto;
        }

        public static bool MotoResultWithGas(int DispatchId)
        {
            bool isQualify = false;
            try
            {
                string strSQL = string.Format("SELECT  JCLSH,Z_PD FROM  RESULT_VEHICLE_INFO WHERE ID = {0}", DispatchId);
                DataTable dt = new DataTable();
                DbHelper.GetTable(strSQL, ref dt);
                int iZ_PD = Convert.ToInt32(dt.Rows[0]["Z_PD"]);
                string strJCLSH = Convert.ToString(dt.Rows[0]["JCLSH"]);

                List<string> listJCLSH = Tools.UseTools.GetAllJCLSHFromResult(strJCLSH);

                RESULT_SDS_BLL bll = new RESULT_SDS_BLL();

                RESULT_SDS sdsEntity = new RESULT_SDS();
                RESULT_SDS t_result_sds = new RESULT_SDS();

                for (int i = 0; i < listJCLSH.Count; i++)
                {
                    t_result_sds = bll.GetModel(listJCLSH[i]);
                    if (i == 0)
                    {
                        sdsEntity = t_result_sds;
                    }
                    Tools.UseTools.AutoMapping(t_result_sds, sdsEntity);
                }

   
                int j = 0;
                if (sdsEntity.GDSHC_PD == "3" && sdsEntity.GDSCO_PD == "3")
                {

                    if (Convert.ToDouble(sdsEntity.DSCO) < 2.0 && Convert.ToDouble(sdsEntity.DSHC) < Convert.ToDouble(sdsEntity.DSHCXZ))
                    {
                        sdsEntity.SDS_PD = "1";
                    }
                    else
                    {
                        sdsEntity.SDS_PD = "2";
                    }

                }
                else
                {
                    if (Convert.ToDouble(sdsEntity.DSCO) < 2.0 && Convert.ToDouble(sdsEntity.DSHC) < Convert.ToDouble(sdsEntity.DSHCXZ)
                         && Convert.ToDouble(sdsEntity.GDSCO) < Convert.ToDouble(sdsEntity.GDSCOXZ) && Convert.ToDouble(sdsEntity.GDSHC) < Convert.ToDouble(sdsEntity.GDSHCXZ))
                    {
                        sdsEntity.SDS_PD = "1";
                    }
                    else
                    {
                        sdsEntity.SDS_PD = "2";
                    }
                }


                if (int.TryParse(sdsEntity.SDS_PD, out j))
                {
                    if (j != 2 && iZ_PD != 2)
                    {
                        isQualify = true;
                    }
                }
                else
                {
                    if (iZ_PD == 1)
                    {
                        isQualify = true;
                    }
                }

            }
            catch (Exception ex)
            {

            }
            return isQualify;
        }



        public static void Write_Print_Log()
        {

        }

        public static EMPLOYEE_USER GetLatestOperatorByLine(int Line)
        {
            EMPLOYEE_USER entity = null;
            string strSQL = string.Format("SELECT * FROM EMPLOYEE_USER WHERE EMPLOYEE_USER.EMPLOYEE_NAME IN (SELECT TOP 1 YCY FROM RESULT_VEHICLE_INFO WHERE JCXH = {0}  ORDER BY ID DESC)", Line);
            DataTable dt = new DataTable();
            DbHelper.GetTable(strSQL, ref dt);
            if (dt.Rows.Count > 0)
            {
                entity = new EMPLOYEE_USER();
                entity.EMPLOYEE_NAME = Convert.ToString(dt.Rows[0]["EMPLOYEE_NAME"]);
                entity.EMPLOYEE_ID = Convert.ToString(dt.Rows[0]["EMPLOYEE_ID"]);

            }
            return entity;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="JCLSH">检测流水线号</param>
        /// <param name="logMsg">日志消息</param>
        /// <param name="operatorName">操作员名称</param>
        /// <param name="type">日志类型</param>
        /// <param name="funName">方法名称</param>
        public static void WriteLogMsg(string JCLSH, string logMsg, string operatorName, LogHelper.LogType type, string funName)
        {
            try
            {
                #region 日志记录
                LogHelper logHelper = LogHelper.GetSingleLogHelper();
                string strCategory = LogHelper.LogType.Normal.ToString();
                LOG_INFO logInfo = new LOG_INFO();
                logInfo.Category = strCategory;
                logInfo.HostName = System.Net.Dns.GetHostName();
                logInfo.IPAddress = System.Net.Dns.GetHostAddresses(logInfo.HostName).GetValue(1).ToString();
                logInfo.LogTime = DateTime.Now;

                logInfo.JCLSH = JCLSH;
                logInfo.Log_Infor = logMsg;
                logInfo.OpName = operatorName;
                logInfo.Name = logHelper.GetLogName(type);
                logInfo.FunName = funName;

                logHelper.WriteLog(logInfo);
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


    }
}